<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_peimilk/model')->方法名();
*/
namespace jy_peimilk;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function up_pass_sendlog(){
		$sendlogs=$this->getall('sendlog',array('daytime'=>0,'uniacid'=>$_SESSION['uniacid']));
		if($sendlogs){
			foreach($sendlogs as $k=>$g){
				$this->update('sendlog',array('daytime'=>strtotime($g['day'])),array('id'=>$g['id'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
		$this->update('sendlog',array('status'=>7),array('daytime <'=>time()-24*3600,'status'=>0,'uniacid'=>$_SESSION['uniacid']));
		return false;
	}
	public function get_peifee($sendlog=array()){
		$mid=$mid?:$_SESSION['uid'];
		$level_id=$this->getcolumn('member',array('mid'=>$sendlog['mid'],'uniacid'=>$_SESSION['uniacid']),'level_id');
		if($level_id){
			$level=$this->get('level',array('id'=>$level_id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			if($sendlog['merchid']){
				$level=$this->plugin_setting('default_level'.$sendlog['merchid']);
			}else{
				$level=$this->plugin_setting('default_level');
			}
		}
		$fee_par=$level['fee_par'];
		$this->needorder=false;
		$getfee=0;
		$num=0;
		$peisetgoods=$sendlog['goods'];
		if(!is_array($sendlog['goods'])){
			$peisetgoods=$sendlog['goods']?json_decode($sendlog['goods'],true):array();
		}
		if($peisetgoods){
			foreach($peisetgoods as $k=>$g){
				$num +=$g['num'];
			}
			foreach($peisetgoods as $g){
					$ignore=$this->get('goods',array('goods_id'=>$g['goodsid'],'uniacid'=>$_SESSION['uniacid']));
					if(!$ignore['id']){
						continue;
					}
					$num -=$g['num'];
					if($ignore['ignore']){
						continue;
					}else{
						$goods_commission=$ignore['commissions']?json_decode($ignore['commissions'],true):array();
					}
					foreach($goods_commission as $gc){
						if($gc['level_id']!=$level_id){
							continue;
						}else{
							$goods_per=$gc['fee_per'];
							if(substr($goods_per,-1)=='%'){
								$getfee +=($g['price']*substr($goods_per,0,-1))/100;
							}else{
								$getfee +=$goods_per;
							}
							break;
						}
					}
			}
		}
		if($this->peisetting['peifeetype']){
			//按商品金额百分比计算
		}else{
			//按商品个数计算
		}
		if($num){
			$getfee +=$num*$fee_par;
		}
		return $getfee;
	}
	public function up_allgoodsset($mid=0,$merchid=0){
		$mid=$mid?:$_SESSION['uid'];
		$allgoods=array();
		$params=array('istaocan'=>1,'ispay'=>1,'status'=>array(1,2,3,10,11,12,13),'mid'=>$mid,'merchid'=>$merchid);
		$ordergoods=pdo_getall('jy_weishop_order_goods',$params,array('mid','merchid','istaocan','goodsid','optionid','orderid','price','total','goodsnum'));
		foreach($ordergoods as $k=>&$g){
				$goodskey=$g['goodsid'].'_'.$g['optionid'];
				if($allgoods[$goodskey]){
					continue;
				}else{
					$goods=pdo_get('jy_weishop_goods',array('id'=>$g['goodsid']));
					$g['unit']=$goods['unit'];
					$g['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$g['goodsid']),'weight');
					if($g['optionid']){
						$options=pdo_get('jy_weishop_goods_option',array('id'=>$g['optionid']),array('title','specs'));
						if($g['istaocan']==1){
								$selespename = $options['title'];
						}else{
								$specs=$options['specs'];
								$g['price']=pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$g['goodsid'],'id'=>$g['optionid']),'price');
								$specs=explode("_",$specs);
								foreach($specs as &$spec){
									$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
								}
								$optiontitle=implode("，",$specs);
								$selespename = $optiontitle;
						}
						$g['optiontitle']=$g['selespename']= $selespename;			
					}
					$g['title']=$goods['title'];
					$allgoods[$goodskey]=$g;
				}
		}
		foreach($allgoods as &$av){
			$this->check_goodsset($av);
		}
		$this->model('order')->up_ordergoods($mid,$merchid);
		return $allgoods;
	}
	public function check_goodsset($goods=array(),$isupdate=false,$fromsetmid=false){
		$goodsset=$this->pdo_get('goodsset',array('optionid'=>$goods['optionid'],'goodsid'=>$goods['goodsid'],'mid'=>$goods['mid'],'merchid'=>$goods['merchid']));
		$goodsset['numset']=json_decode($goodsset['numset'],true);
		$api_data=pdo_getcolumn('jy_weishop_goods',array('id'=>$goods['goodsid']),'api_data');
		$api_data=json_decode($api_data,true);
		$leixings=$api_data['leixings'];
		if(!count($leixings)){
			$leixings[]=array(
				'title'=>'默认类型',
				'data'=>array(
					array('id'=>0,'num'=>''),
					array('id'=>1,'num'=>''),
					array('id'=>2,'num'=>''),
					array('id'=>3,'num'=>''),
					array('id'=>4,'num'=>''),
					array('id'=>5,'num'=>''),
					array('id'=>6,'num'=>''),
				)
			);
		}
		$numset=$goods['numset']?:$goodsset['numset'];
		if(!$numset || !$numset[0]['data']){
			foreach($leixings as &$lv){
				$lv['data']=array(
					array('id'=>0,'num'=>''),
					array('id'=>1,'num'=>''),
					array('id'=>2,'num'=>''),
					array('id'=>3,'num'=>''),
					array('id'=>4,'num'=>''),
					array('id'=>5,'num'=>''),
					array('id'=>6,'num'=>''),
				);
			}
			$numset=$leixings;
		}
		$params=array('optionid'=>$goods['optionid'],'goodsid'=>$goods['goodsid'],'istaocan'=>1,'ispay'=>1,'status'=>array(1,2,3,10,11,12,13),'mid'=>$goods['mid'],'merchid'=>$goods['merchid']);
		$order_goods=pdo_getall('jy_weishop_order_goods',$params,array('total','goodsnum'));
		$allnum=0;
		foreach($order_goods as $ov){
				$ov['goodsnum']=$ov['goodsnum']?:1;
				$ov['total']=$ov['total']?:1;
				$allnum +=$ov['goodsnum']*$ov['total'];
		}
		$data=array(
			'set_mid'=>$_SESSION['uid'],
			'title'=>$goods['title']?:$goodsset['title'],
			'selespename'=>$goods['selespename']?:$goodsset['selespename'],
			'numset'=>ijson_encode($numset),
			'allnum'=>$allnum,
			'lastnum'=>$allnum-$goodsset['issendnum'],
			'uptime'=>time(),
		);
		if($allnum<=0){
			if($goodsset['id']){
				$this->pdo_delete('goodsset',array('id'=>$goodsset['id'],'uniacid'=>$_SESSION['uniacid']));
			}
			return false;
		}
		if(!$goodsset['id']){
			$data['uniacid']=$_SESSION['uniacid'];
			$data['mid']=$goods['mid'];
			$data['merchid']=$goods['merchid'];
			$data['goodsid']=$goods['goodsid'];
			$data['optionid']=$goods['optionid'];
			$data['lastnum']=$allnum;
			$this->insert('goodsset',$data);
			$goodsset['id']=$this->pdo_insertid();
		}else{
			if(!$isupdate){
				unset($data['set_mid']);
				unset($data['uptime']);
			}
			//非配送员设置
			if(!$fromsetmid){
				$this->pdo_update('goodsset',$data,array('id'=>$goodsset['id']));
			}
		}
		$goodsset=$this->pdo_get('goodsset',array('id'=>$goodsset['id'],'uniacid'=>$_SESSION['uniacid']));
		return $goodsset;
	}
	/*
		更新已配送数量
	*/
	public function up_goodsnum($sendlog,$isjian=false){
		$peisetgoods=json_decode($sendlog['goods'],true);
		foreach($peisetgoods as $k=>$g){
			if($g['num']>0){
				$this->pdo_update('goodsset',array('issendnum +='=>$g['num'],'lastnum -='=>$g['num']),array('optionid'=>$g['optionid'],'goodsid'=>$g['goodsid'],'mid'=>$sendlog['buymid'],'merchid'=>$sendlog['merchid']));
			}
		}
		return false;
	}
	/*
		异常同步已配送数量
	*/
	public function up_goodsnum2($mid=0,$merchid=0){
		$params=array('is_dingpei'=>1,'status'=>2,'uniacid'=>$_SESSION['uniacid'],'buymid'=>$mid,'merchid'=>$merchid);
		$list=pdo_getall('jy_peimilk_sendlog',$params);
		$goodsset=array();
		foreach($list as $item){
			$peisetgoods=json_decode($item['goods'],true);
			foreach($peisetgoods as $k=>$g){
				$key=$g['goodsid'].'_'.$g['optionid'];
				if(!$goodsset[$key]){
					$goodsset[$key]=$g;
				}else{
					$goodsset[$key]['num'] +=$g['num'];
				}
			}
		}
		foreach($goodsset as $g){
			$this->pdo_update('goodsset',array('issendnum'=>$g['num']),array('optionid'=>$g['optionid'],'goodsid'=>$g['goodsid'],'mid'=>$mid,'merchid'=>$merchid));
		}
		return false;
	}
	public function up_orderuser($mid=0,$merchid=0,$peisong_id=0,$ischeck=false){
		$mid=$mid?:$_SESSION['uid'];
		$orderuser=$this->get('orderuser',array('mid'=>$mid,'merchid'=>$merchid));
		if($orderuser['id']){
			if($peisong_id){
				if($peisong_id=='no'){
					$peisong_id=0;
				}elseif($ischeck && $orderuser['peisong_id']){
					$sendnum=$this->pdo_count('sendlog',array('status'=>array(0,1),'buymid'=>$mid,'merchid'=>$merchid));
					if($sendnum>0){
						return $orderuser;
					}
				}
				if(!$peisong_id){
					$this->pdo_delete('sendlog',array('status'=>array(0,1),'buymid'=>$mid,'merchid'=>$merchid));	
				}else{
					$this->pdo_update('sendlog',array('mid'=>$peisong_id),array('status'=>array(0,1),'buymid'=>$mid,'merchid'=>$merchid));
				}
				$this->update('orderuser',array('peisong_id'=>$peisong_id),array('id'=>$orderuser['id'],'uniacid'=>$_SESSION['uniacid']));
				$orderuser['peisong_id']=$peisong_id;
			}
		}else{
			$orderuser=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'merchid'=>$merchid,
				'peisong_id'=>$peisong_id,
				'createtime'=>time(),
			);
			$this->insert('orderuser',$orderuser);
			$orderuser['id']=$this->pdo_insertid();
		}
		return $orderuser;
	}
	public function get_logaddress($sendlog=array()){
		$peiset=$this->pdo_get('peiset',array('order_id'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$sendlog['buymid'],'merchid'=>$sendlog['merchid']));
		if($peiset['id']){
			$peiset['address']=json_decode($peiset['address'],true);
			if($peiset['address']['isopen']==1){
				return $peiset['address'];
			}
		}
		$order=pdo_get('jy_weishop_order',array('mid'=>$sendlog['buymid'],'merchid'=>$sendlog['merchid'],'ispay'=>1,'is_dingpei'=>1),array('address','sendtype','addressid','is_dingpei'),'id desc');
		$order['address']=json_decode($order['address'],true);
		if($order['address'] && ($order['address']['name'] || $order['address']['realname'])){
			$address=$order['address'];
		}else{
			$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['addressid']));
		}
		if(!is_array($address['pcd'])){
			$address['pcd']=json_decode($address['pcd'],true);
		}
		return $address;
	}
}
?>